<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\controller\Handle;
use app\common\model\FileLog;

use think\facade\View;
use think\facade\Config;
use think\facade\Cache;


class Upload extends Base
{

    //上传图片
    public function uploadImg()
    {
        $param = request()->param();
        $hand = new Handle();
        $upload_type = config('sysconfig.upload_type');
        $file_type = $param['type'] ?? 0;
        $dirname = uploadType($file_type);
        // var_dump($upload_type);
        if ($upload_type == 1) {
            // 本地上传
            // die;
            $file_type_name = 'file';
            if ($file_type == 'Images') {
                $file_type_name = 'upload';
                $file_type = 3; //后台富文本
                $dirname = 'content';
            }
            $info = $hand->commonUpload($file_type_name, $dirname);
        } else if ($upload_type == 2) {
            // 七牛上传
            // $file_type_name = 'file';

            // if($file_type == 'Images'){
            //     $file_type_name = 'upload';
            //     $file_type=3;//后台富文本
            //     $dirname = 'content';
            // }
            // $info = $hand->qiniuImg($file_type_name,$dirname);
            $info = $hand->qiniuImg();

            if ($info['status']) {
                if (isset($param['do'])) {
                    //ckea编辑器固定返回格式
                    return json(['uploaded' => 1, 'fileName' => $info['qnname'], 'url' => 'https://qiniu.0315678.cn' . $info['qnname']]);
                }
                $str = '';
                if (isset($param['type'])) {
                    $str = $param['type'];
                }
                $re['imgpath'] = $info['qnname'];
                return json(['code' => 1, 'data' => $re, 'typestr' => $str, 'imgurl' => 'https://qiniu.0315678.cn' . $info['qnname']]);
            } else {
                return json(['code' => 0, 'msg' => '上传失败', 'phpmsg' => $info['msg']]);
            }
        }
        // var_dump($info);
        // 写入上传记录
        if ($info['status'] == 1) {
            $addlog = [];
            $addlog['file_name'] = $info['fileName'];
            $addlog['upload_type'] = $upload_type;
            $addlog['file_path'] = $info['url'];
            $addlog['file_type'] = $file_type;
            $addlog['ip'] = getRealIP();
            $addlog['admin_user_id'] = session('admin_user');
            $addlog['add_time'] = time();
            $log = FileLog::create($addlog);
            $info['file_id'] = $log->id;
        }

        return json($info);
        // $param = request()->param();
        // if ($info['status']) {
        //     if (isset($param['do'])) {
        //         //ckea编辑器固定返回格式
        //         return json(['uploaded' => 1, 'fileName' => $info['qnname'], 'url' => config('sysconfig.qiniu_domain') . $info['qnname']]);
        //     }
        //     $str = '';
        //     if (isset($param['type'])) {
        //         $str = $param['type'];
        //     }
        //     $re['imgpath'] = $info['qnname'];
        //     return json(['code' => 1, 'data' => $re, 'typestr' => $str, 'imgurl' => config('sysconfig.qiniu_domain') . $info['qnname']]);
        // } else {
        //     return json(['code' => 0, 'msg' => '上传失败', 'phpmsg' => $info['msg']]);
        // }
    }
}
